import { GCtrl } from "./GCtrl";

class _Padding {
    left = 3;
    right = 3;
    top = 3;
    bottom = 3;
}
var Padding = cc.Class(
    {
        name: 'Padding',
        properties: new _Padding
    }
)
const { ccclass, property } = cc._decorator;

@ccclass
export default class NGrid extends cc.Component {

    @property(Padding)
    padding = new _Padding;

    @property(cc.Float)
    spacingX: number = 3;
    @property(cc.Float)
    spacingY: number = 3;
    // @property(cc.Integer)
    // _horMaxNum: number = 3;
    // @property
    // get horMaxNum() {
    //     return this._horMaxNum;
    // }
    // set horMaxNum(value) {
    //     if (typeof value != 'number') return;
    //     this._horMaxNum = Math.floor(value);
    // }
    // @property(cc.Integer)
    // _verMaxNum: number = 3;
    // @property
    // get verMaxNum() {
    //     return this._verMaxNum;
    // }
    // set verMaxNum(value) {
    //     if (typeof value != 'number') return;
    //     this._verMaxNum = Math.floor(value);
    // }
    //---------------------
    private items: cc.Node[] = null;
    private isGrid: boolean = false;

    onLoad() {
        GCtrl.eventIndex['updateLayoutGrid' + this.node.uuid] = cc.vv.eventCustom.on('updateLayoutGrid', this.layoutGrid.bind(this), this.node);
        this.items = this.node.children;
    }
    onEnable() {
    }
    onDisable() {
    }
    onDestroy() {
        GCtrl.eventIndex['updateLayoutGrid' + this.node.uuid] && (cc.vv.eventCustom.off('updateLayoutGrid', GCtrl.eventIndex['updateLayoutGrid' + this.node.uuid]), delete GCtrl.eventIndex['updateLayoutGrid' + this.node.uuid]);
    }
    start() {
        this.schedule(this.checkGrid.bind(this), 0, cc.macro.REPEAT_FOREVER);
    }

    // update(dt) { }
    private resetItems() {
        for (let i in this.items) {
            this.items[i].setPosition(cc.v2());
        }
    }
    private checkGrid() {
        if (this.isGrid) {
            this.unscheduleAllCallbacks();
            return;
        }
        else {
            this.layoutGrid();
        }
    }

    layoutGrid() {
        let length = this.items.length;
        if (!length) return;
        // this.resetItems();
        let width = this.node.width;
        let height = this.node.height;
        let n = Math.sqrt(length);
        let t = Math.floor(n);
        n > t && (n += 1);
        n = Math.floor(n);
        let hor = n;
        let ver = n;
        length == 2 && (ver = 1, hor = 2);
        let mW = (width - this.padding.left - this.padding.right - this.spacingX * (hor - 1)) / hor;
        let mH = mW;
        //(height - this.padding.top - this.padding.bottom - this.spacingY * (ver - 1)) / ver;
        let b = false;
        let nilNum = hor * ver - length;
        let l_0: cc.Node[] = [];
        for (let i = 0, k = 0; i < ver; ++i) {
            let y = height * this.node.anchorY - this.padding.top - mH * this.items[k].anchorY - i * (this.spacingY + mH);
            let x = -width * this.node.anchorX + this.spacingX + mW * this.items[k].anchorX;
            for (let j = 0; j < hor; ++j) {
                this.items[k].width = mW;
                this.items[k].height = mH;
                this.items[k].setPosition(cc.v2(x, y));
                x += this.spacingX + mW;
                !nilNum && ++k;
                nilNum && --nilNum;
                if (k == length) {
                    b = true;
                    break;
                }
            }
            if (b) break;
        }
        //第一排居中
        let a = this.items.length % hor;
        for (let i = 0; i < a; ++i) {
            l_0.push(this.items[i]);
        }
        if (l_0.length > 0) {
            let b_n = l_0[0];
            let e_n = l_0[l_0.length - 1];
            let b_x = b_n.x - b_n.width * b_n.anchorX;
            let e_x = e_n.x + e_n.width * (1 - e_n.anchorX);
            let w = Math.abs(e_x - b_x);
            w = width - w;
            w /= 2;
            b_x = -width * this.node.anchorX + w + mW * b_n.anchorX - b_n.x;
            for (let i = 0; i < l_0.length; ++i) {
                l_0[i].x += b_x;
            }
        }
        //剩下排居中
        let b_n = this.items[a];
        let e_n = this.items[a + hor - 1];
        let b_x = b_n.x - b_n.width * b_n.anchorX;
        let e_x = e_n.x + e_n.width * (1 - e_n.anchorX);
        let w = Math.abs(e_x - b_x);
        w = width - w;
        w /= 2;
        b_x = -width * this.node.anchorX + w + mW * b_n.anchorX - b_n.x;
        for (let i = a; i < this.items.length; ++i) {
            this.items[i].x += b_x;
        }
        //垂直居中
        b_n = this.items[0];
        e_n = this.items[length - 1];
        let b_y = b_n.y + b_n.height * b_n.anchorY;
        let e_y = e_n.y - e_n.height * (1 - e_n.anchorY);
        let h = Math.abs(e_y - b_y);
        h = height - h;
        h /= 2;
        b_y = height * this.node.anchorY - h - mH * b_n.anchorY - b_n.y;
        for (let i = 0; i < length; ++i) {
            this.items[i].y += b_y;
        }
        this.isGrid = true;
    }
}
